package org.example.struct.array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2023/12/7 09:45
 * @apiNote
 */

public class LCR186CheckDynasty {

    public static void main(String[] args) {
        int[] places = {9,2,6,7,10};
        System.out.println(test(places));
    }

    public static boolean test(int[] places) {
        Arrays.sort(places);
        int n = places.length;
        int max = places[n - 1];
        int min = max;
        for (int i = 0; i < n; i++) {
            if (places[i] != 0) {
                if (i != 0 && places[i] == places[i - 1]) {
                    return false;
                }
                min = Math.min(min, places[i]);
            }
        }
        return max - min < 5;
    }

    public static boolean test2(int[] places) {
        Arrays.sort(places);
        int n = places.length;
        int count = 0;
        for(int i = 0; i < n-1; i++){
            if(places[i] == 0){
                count++;
            }else if(places[i] == places[i+1]){
                return false;
            }
        }
        return places[4] - places[count] < 5;
    }

    public static boolean checkDynasty(int[] places) {
        int n = places.length;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (places[i] == 0) {
                continue;
            }
            if (set.contains(places[i])) {
                return false;
            }
            set.add(places[i]);
            max = Math.max(max, places[i]);
            min = Math.min(min, places[i]);
        }
        return max - min < 5;
    }
}
